var MaterialColorConfigModel = function (data) {
  BaseModel.call(this);

  var self = this;
  var fn = {
    initData: function (data) {
      self.minHeight = data.minHeight;

      var remarkList = data.remarkList;
      if (SysUtil.isEmpty(remarkList)) {
        remarkList = [];
      }
      self.remarkList = remarkList;

      var fieldObj = data.fieldObj;
      var allFields = MaterialColorConfigModel.fields;
      var fieldList = [];
      for (var i = 0; i < allFields.length; i++) {
        var allField = allFields[i];
        var id = allField.id;
        var fieldConfig = fieldObj[id];
        if (fieldConfig === undefined) {
          fieldConfig = {
            isShow: false,
            width: 100
          };
        }
        var obj = _.cloneDeep(fieldConfig);
        obj.id = id;
        if (obj.width === undefined) {
          obj.width = null;
        }
        var label = allField.label;
        obj.label = label;
        fieldList.push(obj);
      }
      self.fieldList = fieldList;

      var materialTypeConfig = data.materialType;
      self.materialTypeConfig = materialTypeConfig;

      var glassesConfig = data.glasses;
      self.glassesConfig = glassesConfig;

      var deliveryDateConfig = data.deliveryDate;
      self.deliveryDateConfig = deliveryDateConfig;
    },

    resetSingeDataWidth: function (field, newWidth, oldWidth) {
      var fieldList = self.fieldList;
      var fieldObj = _.find(fieldList, {
        id: field
      });
      if (fieldObj === undefined) {
        return;
      }
      this.setNewWidth(fieldObj, newWidth, oldWidth);
    },

    resetMaterialTypeDataWidth: function (arr, newWidth, oldWidth) {
      var materialTypeConfig = self.materialTypeConfig;
      var materialType = arr[1];
      var material = _.find(materialTypeConfig, {
        materialType: materialType
      });
      if (material === undefined) {
        return;
      }

      var field = arr[2];
      var columns = material.columns;
      var column = _.find(columns, {
        id: field
      });
      if (column === undefined) {
        return;
      }
      this.setNewWidth(column, newWidth, oldWidth);
    },

    resetGlassesDataWidth: function (arr, newWidth, oldWidth) {
      var glassesConfig = self.glassesConfig;
      var glassesType = arr[1];
      var glasses = glassesConfig[glassesType];
      if (glasses === undefined) {
        return;
      }
      this.setNewWidth(glasses, newWidth, oldWidth);
    },

    resetDeliveryDateDataWidth: function (arr, newWidth, oldWidth) {
      var deliveryDateConfig = self.deliveryDateConfig;
      var deliveryDate = deliveryDateConfig[arr[1]];
      if (deliveryDate === undefined) {
        return;
      }

      var columns = deliveryDate.columns;
      var column = _.find(columns, {
        id: arr[2]
      });
      if (column === undefined) {
        return;
      }
      this.setNewWidth(column, newWidth, oldWidth);
    },

    setNewWidth: function (obj, newWidth, oldWidth) {
      var width = obj.width;
      var rangeWidth = newWidth - oldWidth;
      width = width / oldWidth * rangeWidth;
      width = _.round(width, 0);
      obj.width += width;
    }
  };

  this.resetData = function (data) {
    this.minHeight = data.minHeight;

    var fieldList = data.fieldList;
    var fields = [];
    for (var i = 0; i < fieldList.length; i++) {
      var fieldObj = fieldList[i];
      fields.push({
        id: fieldObj.id,
        label: fieldObj.label,
        isShow: fieldObj.checked,
        width: fieldObj.width
      });
    }
    this.fieldList = fields;

    var materialTypeConfig = data.materialTypeConfig;
    var materialList = [];
    for (var i = 0; i < materialTypeConfig.length; i++) {
      var material = materialTypeConfig[i];
      var columns = material.columns;
      columns = _.map(columns, function (column) {
        return {
          id: column.id,
          width: column.width,
          isShow: column.checked
        };
      });
      materialList.push({
        materialType: material.materialType,
        columns: columns
      });
    }
    this.materialTypeConfig = materialList;

    var glassesConfig = data.glassesConfig;
    var glassesObj = {};
    for (var i = 0; i < glassesConfig.length; i++) {
      var glasses = glassesConfig[i];
      var isShow = glasses.checked;
      var width = glasses.width;
      if (!isShow && width === undefined) {
        continue;
      }
      glassesObj[glasses.label] = {
        isShow: isShow,
        width: width
      };
    }
    this.glassesConfig = glassesObj;

    var deliveryDateConfig = data.deliveryDateConfig;
    var deliveryDateList = [];
    for (var i = 0; i < deliveryDateConfig.length; i++) {
      var obj = deliveryDateConfig[i];
      var columns = obj.columns;
      columns = _.map(columns, function (column) {
        return {
          id: column.id,
          width: column.width,
          isShow: column.checked
        };
      });
      deliveryDateList.push({
        columns: columns
      });
    }
    this.deliveryDateConfig = deliveryDateList;
  };

  this.resetWidth = function (columnKey, newWidth, oldWidth) {
    if (columnKey.indexOf('_') === -1) {
      fn.resetSingeDataWidth(columnKey, newWidth, oldWidth);
      return;
    }

    var arr = columnKey.split('_');
    var type = arr[0];
    if (type === 'material') {
      fn.resetMaterialTypeDataWidth(arr, newWidth, oldWidth);
    } else if (type === 'glasses') {
      fn.resetGlassesDataWidth(arr, newWidth, oldWidth);
    } else if (type === 'deliveryDate') {
      fn.resetDeliveryDateDataWidth(arr, newWidth, oldWidth);
    }
  };

  this.getObject = function (productOrderData) {
    var postData = {
      minHeight: this.minHeight
    };

    var fieldList = this.fieldList;
    var fieldObj = {};
    for (var i = 0; i < fieldList.length; i++) {
      var obj = fieldList[i];
      var id = obj.id;
      fieldObj[id] = {
        isShow: obj.isShow,
        width: obj.width
      };
    }
    postData.fieldObj = fieldObj;

    var materialTypeConfig = this.materialTypeConfig;
    var materialTypeList = [];
    for (var i = 0; i < materialTypeConfig.length; i++) {
      var obj = materialTypeConfig[i];
      var columns = obj.columns;
      var columnList = [];
      for (var j = 0; j < columns.length; j++) {
        var columnObj = columns[j];
        var isShow = columnObj.isShow;
        var width = columnObj.width;
        if (isShow || width !== undefined) {
          columnList.push({
            id: columnObj.id,
            isShow: isShow,
            width: width
          });
        }
      }
      materialTypeList.push({
        materialType: obj.materialType,
        columns: columnList
      });
    }
    postData.materialType = materialTypeList;

    var glasses = {};
    var glassesConfig = this.glassesConfig;
    for (var materialType in glassesConfig) {
      var obj = glassesConfig[materialType];
      var isShow = obj.isShow;
      var width = obj.width;
      if (isShow || width !== undefined) {
        glasses[materialType] = {
          isShow: isShow,
          width: width
        };
      }
    }
    postData.glasses = glasses;

    var deliveryDateConfig = this.deliveryDateConfig;
    var deliveryDateList = [];
    for (var i = 0; i < deliveryDateConfig.length; i++) {
      var obj = deliveryDateConfig[i];
      var columns = obj.columns;
      var columnList = [];
      for (var j = 0; j < columns.length; j++) {
        var columnObj = columns[j];
        var isShow = columnObj.isShow;
        var width = columnObj.width;
        if (isShow || width !== undefined) {
          columnList.push({
            id: columnObj.id,
            isShow: isShow,
            width: width
          });
        }
      }
      deliveryDateList.push({
        columns: columnList
      });
    }
    postData.deliveryDate = deliveryDateList;

    var remarkList = productOrderData.getRemarkList();
    postData.remarkList = remarkList;

    return postData;
  };

  fn.initData(data);
};

MaterialColorConfigModel.fields = [{
  id: "customerColor",
  label: "客户色号"
}, {
  id: "customerColorDescription",
  label: "客户颜色描述"
}, {
  id: "material",
  label: "物料类型"
}, {
  id: "glasses",
  label: "老花镜"
}, {
  id: "deliveryDate",
  label: "交期"
}, {
  id: "orderTotal",
  label: "订单数"
}, {
  id: "feedTotal",
  label: "投料数"
}, {
  id: "yltsTotal",
  label: "用料数"
}, {
  id: "remark",
  label: "备注"
}];

MaterialColorConfigModel.materialTypeFields = [{
  id: "materialName",
  label: "物料名称"
}, {
  id: "materialModel",
  label: "规格"
}, {
  id: "materialProviderName",
  label: "供应商"
}, {
  id: "materialColor",
  label: "色号"
}, {
  id: "materialColorDescription",
  label: "颜色描述"
}, {
  id: "orderTotal",
  label: "订单数"
}, {
  id: "feedTotal",
  label: "投料数"
}, {
  id: "yltsTotal",
  label: "用料数"
}, {
  id: "remark",
  label: "说明"
}];

MaterialColorConfigModel.deliveryDateFields = [{
  id: "date",
  label: "日期"
}, {
  id: "count",
  label: "数量"
}];

MaterialColorConfigModel.textAlignCenter = ['lineNo', 'orderTotal', 'feedTotal', 'yltsTotal'];
